package com.example.myapplication4;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.app.AbilityContext;
import ohos.app.Context;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.data.distributed.common.UserInfo;
import ohos.data.rdb.ValuesBucket;
import ohos.data.resultset.ResultSet;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import ohos.rpc.*;
import ohos.utils.net.Uri;
import ohos.utils.zson.ZSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

//此Service类用于处理用户房屋破坏情况的上传操作
public class DemoService5 extends Ability {
    private AbilityContext abilityContext;

    MyRemote myRemote = new MyRemote();
    //日志
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");


    @Override
    protected IRemoteObject onConnect(Intent intent) {

        //返回RemoteObject的对象
        return myRemote.asObject();
    }

    class MyRemote extends RemoteObject implements IRemoteBroker {
        private AbilityContext abilitycontext1;

        public MyRemote(String descriptor, AbilityContext abilitycontext1) {
            super(descriptor);
            this.abilitycontext1 = abilitycontext1;
        }

        public MyRemote() {
            super("descriptor");
        }

        //在此函数中进行js与java的连通
        @Override
        public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException {

            switch (code) {
                case 300:
//                    InsertUserDataService ind=new InsertUserDataService();

                    int resu = InsertUserData(data);
                    Map<String, Object> result = new HashMap<String, Object>();
                    if(resu!=0){
                        result.put("ans", "success");

                    }else{
                        result.put("ans", "fail");
                    }
                    reply.writeString(ZSONObject.toZSONString(result));
                    System.out.println(resu);
                    break;

                default:
                    return false;
            }
            return true;
        }

        //向数据库的数据表中插入用户填写的房屋破坏信息
        private int InsertUserData(MessageParcel data) {
            int d = -1;
            String str = data.readString();
            DestroyInfo param=new DestroyInfo();
            try {
                param = ZSONObject.stringToClass(str, DestroyInfo.class);
            } catch (RuntimeException e) {
                System.out.println("failed");
            }
            DataAbilityHelper helper = DataAbilityHelper.creator(DemoService5.this);
            //向数据库中的数据表添加的数据
            ValuesBucket values = new ValuesBucket();
            values.putString("diCat", param.getCategory());
            values.putString("diNam", param.getDisastercat());
            values.putInteger("diTroychengdu", param.getDistroychengdu());
            values.putString("diTime", param.getTimec());
            System.out.println("asfad");
            try {
                //插入数据
                d = helper.insert(Uri.parse("dataability:///com.example.myapplication4.UUDataAbility/destroy"), values);
                System.out.println(d);
            } catch (DataAbilityRemoteException e) {
                e.printStackTrace();
            }
            return d;
        }


        @Override
        public IRemoteObject asObject() {
            return this;
        }

    }
}